package 题目集.dfs;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.LinkedList;

//dfs
//https://pintia.cn/problem-sets/1745607712521764864/exam/problems/1745607748311744525?type=7&page=0
public class 还原文件 {
    static int[] arr;   //缺口
    static int[][] files;   //字片
    static boolean[] visited = new boolean[102];
    static boolean isOver;

    static LinkedList<Integer> res = new LinkedList<>();

    public static void main(String[] args) {
        Reader sc=new Reader();
        int n = sc.nextInt();
        arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        int m = sc.nextInt();
        files = new int[m+1][];
        for (int i = 1; i < files.length; i++) {
            files[i] = new int[sc.nextInt()];
            for (int j = 0; j < files[i].length; j++) {
                files[i][j] = sc.nextInt();
            }
        }
        dfs(0);
        StringBuilder builder=new StringBuilder();
        res.forEach(i->builder.append(i).append(" "));
        builder.deleteCharAt(builder.length()-1);
        System.out.println(builder);
    }

    /**
     * 从缺口的i位置开始找到一条符合的路径
     */
    public static void dfs(int i) {
        if (isOver) return;
        int j = 1;
        for (; j < files.length; j++) {
            if (visited[j]) continue;
            if (compare(i, files[j])) {
                visited[j] = true;
                res.add(j);
                dfs(i + files[j].length - 1);
                if (files.length-1==res.size()) return;
                res.removeLast();
                visited[j] = false;
            }
        }
//        if (visited[j]){    //如果存在一个文件匹配，则继续向下找
//            res.add(j);
//            dfs(i+arr.length-1);
//        }
//        //如果找到了所有的文件
//        if (res.size()==files.length-1) return;
//        res.removeLast();   //删除上一个文件
//        visited[j]=false;   //回溯
    }

    public static boolean compare(int l, int[] file) {
        for (int i = 0; i < file.length; i++) {
            try {
                if (i+l>=arr.length||arr[i + l] != file[i]) {
                    return false;
                }
            } catch (Exception e) {
                System.out.println(l);
            }
        }
        return true;

    }
    static class Reader {
        static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public static int nextInt() {
            try {
                in.nextToken();
            } catch (Exception ignored) {}
            return (int) in.nval;
        }
    }
}

